//
//  HJPostInformationSelectMediaController.m
//  KaiChat
//
//  Created by lx on 2024/3/19.
//

#import "HJSelectMediaController.h"
#import "QMUIButton.h"
#import "HJCheckCell.h"
#import "HJMinePresent.h"
#import "HJUserModel.h"

@interface HJSelectMediaController ()

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UIView *maskView;
@property (nonatomic, weak) QMUIButton *dismissButton;
@property (nonatomic, strong) NSArray *datas;


@end

@implementation HJSelectMediaController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = UIColor.clearColor;
    
    UIButton *maskView = [[UIButton alloc] initWithFrame:self.view.bounds].addTo(self.view);
    maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    [maskView addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    _maskView = maskView;
    
    UIView *contentView = [UIView addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    _contentView = contentView;
    
    UIView *titleView = [UIView addToView:contentView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.right.offset(0);
        make.height.offset(50);
    }];
    
    UILabel *titleLabel = [UILabel lableWithSuperView:titleView font:UIFontMediumMake(16) color:UIColor.tj_333333Color title:@"Issue" textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
    QMUIButton *closeButton = [QMUIButton addToView:titleView];
    [closeButton addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    [closeButton setImage:[UIImage imageNamed:@"nav_close"] forState:UIControlStateNormal];
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.width.height.offset(30);
        make.left.offset(15);
    }];
    
    [self.tableView removeFromSuperview];
    [contentView addSubview:self.tableView];
    [self.tableView registerClass:HJCheckCell.class forCellReuseIdentifier:@"HJCheckCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 50;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(titleView.mas_bottom);
        make.bottom.offset(0);
    }];
    
    CGFloat contentViewH = TabbarSafeBottomMargin + self.datas.count * self.tableView.rowHeight + 50;
    contentView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, contentViewH);
    [contentView addRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight radius:20];
}

- (void)didClickDismissButton {

    [self dismissComplete:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.tj_y = KScreenHeight - self.contentView.tj_height;
        self.maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    } completion:^(BOOL finished) {
    }];
    
}

- (void)dismissComplete:(void (^)(void))complete {
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_y = KScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) {
                complete();
            }
        }];
    }];
}

- (NSArray *)datas {
    if (!_datas) {
        _datas = @[
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.text = @"Photo";
                model.isSelect = [model.text isEqualToString:self.type];
                model.anyObject = @"1";
            }],
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.text = @"Video";
                model.isSelect = [model.text isEqualToString:self.type];
                model.anyObject = @"2";
            }],
        ];
    }
    return _datas;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCheckCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJCheckCell" forIndexPath:indexPath];
    HJCellModel *model = self.datas[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJCellModel *model = self.datas[indexPath.row];
    
    [self.datas enumerateObjectsUsingBlock:^(HJCellModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.isSelect = NO;
    }];
    
    model.isSelect = YES;
    [self.tableView reloadData];
    
    [self dismissComplete:^{
        if (self.complete) {
            self.complete(model);
        }
    }];

}

- (BOOL)hiddenNavigationBar {
    return YES;
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationCustom;
}

- (void)dealloc {
    TJLog(@"%s", __func__);
}

@end


